clear
set more off

local yearlist $yearlist
local varlist1 $varlist1

*Get data
use ${tempdir}/panelreg_inddata_30yr.dta, clear


* Regressions of ever married status
egen firstcode = group(sex first year2)

gen int bpl2 = bpl
replace bpl2 = 9999 if bpl>100

gen byte foreign_born = bpl2>100 if bpl2~=.

gen int spousebpl2 = spousebpl
replace spousebpl2 = 9999 if spousebpl>100

gen byte spouseforeign_born = spousebpl2>100 if spousebpl2~=.

gen byte foreign_parent = mbpl > 100 | fbpl > 100 if mbpl != . & fbpl != . 

gen logoccscore_gap = logoccscore_father_spouse - logoccscore_father
gen abs_agegap = abs(agegap)

gen urb = urban==2 if urban!=.
label var urb "Urban dummy"


foreach y in `yearlist' {
		gen y`y' = year2==`y'
}

forvalues i = 1/4 {
	gen q`i' = qtl_score_wgt_full==`i'
	replace q`i'=. if qtl_score_wgt_full==.
}

forvalues i=1/4 {
	foreach y in `yearlist' {
		gen q`i'_`y' = q`i'*y`y'
	}
}

local dep_ever_married = 1
local dep_logoccscore_dadinlaw_temp = 2
local dep_logoccscore_husband_temp = 3
local dep_agegap = 4

local subset1 ""
local subset2 "& foreign_born != 1"
local subset3 "& foreign_born != 1 & foreign_parent != 1"
local subset4 "& (foreign_born != 1 | spouseforeign_born != 1 )"
local subset5 "& (foreign_born != 1 & spouseforeign_born != 1)"
local subset6 "& spouseforeign_born != 1" 

*local subset3 "& foreign_born != 1 & region_birth_coarse != 3"

gen logoccscore_dadinlaw_temp = logoccscore_father_spouse 
gen logoccscore_husband_temp = log(husboccscore)
gen husb_parent_qtl = qtl_score_wgt_full_spouse

cap postclose results
postfile results subset depvar spec qtile year b se using "$tempdir/reg_robust_exfor.dta", replace

foreach x in 1 2 3 4 5 6{

foreach var of varlist ever_married logoccscore_dadinlaw_temp logoccscore_husband_temp agegap {

* Ever married regressions -- no bpl controls
reg `var' `varlist1' i.age foreign_born if sex==2 `subset`x'', cluster(firstcode)
gen s1_`var'`x' = e(sample)


forvalues i=2/4 {
	*local b`i'_1880 = _b[q`i']
	foreach y in `yearlist' {
		local b`i'_`y' = _b[q`i'_`y']
		local se`i'_`y' = _se[q`i'_`y']
	}
}

forvalues i=2/4 {
	foreach y in `yearlist' {
		post results (`x') (`dep_`var'') (1) (`i') (`y') (`b`i'_`y'') (`se`i'_`y'')
	}
}

* Ever married regressions -- full bpl controls
reg `var' `varlist1' i.age i.bpl2##i.year2 if sex==2 `subset`x'', cluster(firstcode)
gen s2_`var'`x' = e(sample)


forvalues i=2/4 {
	*local b`i'_1880 = _b[q`i']
	foreach y in `yearlist' {
		local b`i'_`y' = _b[q`i'_`y']
		local se`i'_`y' = _se[q`i'_`y']
	}
}

forvalues i=2/4 {
	foreach y in `yearlist' {
		post results (`x') (`dep_`var'') (2) (`i') (`y') (`b`i'_`y'') (`se`i'_`y'')
	}
}


}
}

postclose results

save "${tempdir}reg_data_exfor.dta", replace 

clear 
save "${tempdir}reg_nobs_exfor.dta", replace empty

local dep_ever_married = 1
local dep_logoccscore_dadinlaw_temp = 2
local dep_logoccscore_husband_temp = 3
local dep_agegap = 4

foreach x in 1 2 3 4 5 6{
foreach var in ever_married logoccscore_dadinlaw_temp logoccscore_husband_temp agegap {
foreach spec in 1 2 {

use "${tempdir}reg_data_exfor.dta", clear

keep if s`spec'_`var'`x' == 1

sort year2 first
by year2 first: gen nnames_female = _n == 1

if "`var'" == "logoccscore_dadinlaw_temp" {
sort year2 spousefirst
by year2 spousefirst: gen nnames_male = _n == 1
	}
	
collapse (sum) nobs=s`spec'_`var'`x' nnames_*, by(year2)	

gen subset = `x' 
gen depvar = `dep_`var''
gen spec = `spec'

rename year2 year 

append using "${tempdir}reg_nobs_exfor.dta"
save "${tempdir}reg_nobs_exfor.dta", replace 

}
}
}

use "$tempdir/reg_robust_exfor.dta", clear
merge m:1 subset depvar spec year using "${tempdir}reg_nobs_exfor.dta"

export excel using "$outputdir/Regression Robustness.xlsx", sheet("exfor", replace) firstrow(var)

erase "${tempdir}reg_data_exfor.dta"
erase "${tempdir}reg_nobs_exfor.dta"








